<!--
 * @Author: 奶龙也是龙 15104937352@163.com
 * @Date: 2025-09-11 08:36:57
 * @LastEditors: 奶龙也是龙 15104937352@163.com
 * @LastEditTime: 2025-09-17 15:41:28
 * @FilePath: \大三前端\city\src\pages\components\Left2.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
    <div>
        <Title :title="title"></Title>
        <!-- 1.提供图标容器 -->
        <div ref="bottom4" style="height: 150px;margin-top: 30px;"></div>
    </div>
</template>
<script>
import { Rose } from '@antv/g2plot';
import { get } from '@/utils/request';
import moment from 'moment';
import { mapState } from 'vuex';
export default {
    data() {
        return {
            title: '实时湿度',
            params: {
                page: 1,
                pageSize: 100
            },
            tempData: [],
            rosePlot:null
        }
    },
      computed: {
        ...mapState(['ed'])
    },
    //侦听器 侦听数据
    watch: {
        ed: {
            //根据设备信息重新查询数据重新渲染图表
            handler(n, o) {
                this.params.engineer_id = n.engineer_id;
                this.params.device_id = n.device_id;
                this.getTempData();
                //根据获取的数据重新渲染图表
                this.rosePlot.changeData(this.tempData);
            },
            //深度监听
            deep: true
        }
    },
    methods: {
        loadLine() {
            this.rosePlot = new Rose(this.$refs.bottom4, {
                data:this.tempData,
                xField: 'insert_time',
                yField: 'humidity',
                seriesField: 'insert_time',
                radius: 0.8,
                legend: {
                    position: 'bottom',
                },
            });

            this.rosePlot.render();
        },
        async getTempData() {
            // 调用接口获取今日数据
            let res = await get('/dashboard/pageQueryTodayData', this.params);
            console.log(res, "获取响应");
            let temp = [];
            // 处理数据
            res.data.list.forEach((item, index, arr) => {
                arr[0].humidity = 60;
                // 格式化时间
                item.insert_time = moment(item.insert_time).format("HH:mm");
                // 每隔10条数据取一条，减少数据点数量
                if (index % 15 == 0) {
                    temp.push(arr[index])
                }
            });
            this.tempData = temp; // 保存处理后的数据
        },
    },
    async mounted() {
        await this.getTempData();
        this.loadLine();
    }
}
</script>
<style scoped></style>